//
//  PKDrawingColorOrLineView.m
//  PDFKitTest
//
//  Created by Emo, on 2024/1/29.
//

#import "PKDrawingColorOrLineView.h"

@interface PKDrawingColorOrLineView ()
/** 数据源*/
@property (nonatomic, strong) NSArray * dataSource;
/** 记录上一个按钮*/
@property (nonatomic, strong) UIButton * lastBtn;
/** UI类型*/
@property (nonatomic, assign) PKDrawingSelectType type;

@end

@implementation PKDrawingColorOrLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.type = PKDrawingSelectTypeColor;
        [self setupUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame type:(PKDrawingSelectType)type {
    if (self = [super initWithFrame:frame]) {
        self.type = type;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    NSArray *colorTextArray = @[@"black", @"blue1", @"red", @"blue", @"yellow", @"green", @"purple", @"cyan"];
    NSArray *colorsArray = @[@"#000000", @"#1296DB", @"#FF0000", @"#0000FF", @"#FFFF00", @"#008000", @"#800080", @"#00FFFF"];
    NSArray *lineTextArray = @[@"thin", @"medium", @"blod"];
    NSArray *linesArray = @[@"5", @"15", @"20"];
    NSInteger lineCount = colorTextArray.count / 2;
    CGFloat width = self.width / lineCount;
    CGFloat height = self.height / 2;
    
    self.dataSource = colorsArray;
    if (self.type == PKDrawingSelectTypeLine) {
        lineCount = colorTextArray.count;
        height = self.height;
        self.dataSource = linesArray;
    }
    for (int i = 0; i < self.dataSource.count; i++) {
        NSString *img_n;
        NSString *img_s;
        if (self.type == PKDrawingSelectTypeLine) {
            img_n = [NSString stringWithFormat:@"line_%@_n", lineTextArray[i]];
            img_s = [NSString stringWithFormat:@"line_%@_s", lineTextArray[i]];
        } else {
            img_n = [NSString stringWithFormat:@"color_%@_n", colorTextArray[i]];
            img_s = [NSString stringWithFormat:@"color_%@_s", colorTextArray[i]];
        }
        UIButton *colorBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [colorBtn setImage:[UIImage imageNamed:img_n] forState:(UIControlStateNormal)];
        [colorBtn setImage:[UIImage imageNamed:img_n] forState:(UIControlStateHighlighted)];
        [colorBtn setImage:[UIImage imageNamed:img_s] forState:(UIControlStateSelected)];
        colorBtn.tag = 100 + i;
        [colorBtn addTarget:self action:@selector(colorBtnClick:) forControlEvents:(UIControlEventTouchUpInside)];
        if (i < lineCount) {
            CGFloat x = i * width;
            colorBtn.frame = CGRectMake(x, 0, width, height);
        } else {
            CGFloat x = (i - lineCount) * width;
            colorBtn.frame = CGRectMake(x, height, width, height);
        }
        [self addSubview:colorBtn];
        // 标记第一个按钮
        if (i == 0) {
            colorBtn.selected = YES;
            self.lastBtn = colorBtn;
        }
        // 中间按钮位置调整
        if (self.type == PKDrawingSelectTypeLine && i == self.dataSource.count-2) {
            colorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }
    }
}


- (void)colorBtnClick:(UIButton *)sender {
    if (sender == self.lastBtn) {
        return;
    }
    sender.selected = !sender.selected;
    self.lastBtn.selected = NO;
    if (self.delegate && [self.delegate respondsToSelector:@selector(colorOrLineWithType:value:)]) {
        NSInteger index = sender.tag - 100;
        NSString *value = self.dataSource[index];
        [self.delegate colorOrLineWithType:self.type value:value];
    }
    self.lastBtn = sender;
}

@end
